package pearson.automation.api;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Library API calls.
 */
public class LibraryAPI extends BaseAPICall {

    /**
     * Get a list of assets in user's library.
     * @param userID
     *      User ID
     * @param resultsNumber
     *      Number of returned results
     * @return a list of asset ids
     */
    public final ArrayList<String[]> getItems(
            final String userID,
            final String resultsNumber) {
    info(String.format("getItems(%1$s, %2$s)", userID, resultsNumber));
        String urlTemplate = "rel/api/library";
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("userid", userID);
            parameters.put("results", resultsNumber);
        Document doc = performAPICall(urlTemplate, parameters, false);

        ArrayList<String[]> elements = new ArrayList<String[]>();
        try {
            NodeList nodeList = doc.getElementsByTagName("library_item");
            for (int iIndex = 0; iIndex < nodeList.getLength(); iIndex++) {
                if (nodeList.item(iIndex).getNodeType() == Node.ELEMENT_NODE) {
                    Element element = (Element) nodeList.item(iIndex);
                    String[] elementDetails = new String[2];
                    elementDetails[0] = element.getAttribute("id");
                    elementDetails[1] = element.getAttribute("type");
                    elements.add(elementDetails);
                }
            }
        } catch (Exception e) {
            warn(String.format("exception: %s", e.toString()));
        }
        return elements;
    }

    /**
     * Delete the asset from the user's library.
     * @param userID
     *      user ID
     * @param itemID
     *      Asset ID
     */
    public final void deleteItem(final String userID, final String itemID) {
    info(String.format("deleteItem(%1$s, %2$s)", userID, itemID));
        String urlTemplate = "rel/api/library/delete";
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("userid", userID);
            parameters.put("library_item_id", itemID);

        performAPICall(urlTemplate, parameters, true);
    }

    /**
     * Delete all items in the library.
     * @param userID
     *      User ID
     */
    public final void deleteAllItems(final String userID) {
    info(String.format("deleteAllItems(%1$s)", userID));
        String urlTemplate = "rel/api/user/deleteUserCollections";
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("userid", userID);
        performAPICall(urlTemplate, parameters, true);
    }

    /**
     * Clear front end cache.
     */
    public final void clearCache() {
    info("clear front-end course cache");
        String urlTemplate = "rel/api/clearFECache";
        performAPICall(urlTemplate, null, true);
    }

    /**
     * Add asset to library.
     * @param userID
     *      User ID
     * @param itemID
     *      Asset ID
     */
    public final void addToLibrary(final String userID, final String itemID) {
    info(String.format("addToLibrary(%1$s, %2$s)", userID, itemID));
        String urlTemplate = "rel/api/library/add";
        HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("userid", userID);
            parameters.put("itemUniqueref", itemID);
            parameters.put("title", String.format(
                    "Autotest %1$tm/%1$td/%1$ty %1$tN",
                        Calendar.getInstance()));
        performAPICall(urlTemplate, parameters, true);
    }

    @Override
    protected final String formatLogMsg(final String message) {
        return String.format("LibraryAPI: %s", message);
    }
}
